from django.shortcuts import render

# Create your views here.

# 查询省份信息
# select * from tb_areas where parent_id is null
# GET           areas/infos/


# 查询市
# select * from tb_areas where parent_id=130000
# 查询区县
# select * from tb_areas where parent_id=130600
# GET           areas/infos/pk
from areas.models import Area

"""以上可以定义2个视图来实现"""
"""但是为了我们的学习 使用视图集来实现"""

from rest_framework.viewsets import ReadOnlyModelViewSet
from .serializers import AreaSerializer,AreaSerializer1
from rest_framework_extensions.cache.mixins import ListCacheResponseMixin,RetrieveCacheResponseMixin,CacheResponseMixin


class AreasReadOnluModelViewSer(CacheResponseMixin,ReadOnlyModelViewSet):

    # queryset = Area.objects.all()
    # queryset = Area.objects.filter(parent__isnull=True)  #省份信息

    # 重写这个方法 我们可以根据具体的逻辑  返回不同的查询结果集
    def get_queryset(self):
        if self.action == "list":
        # 获取省份信息
            return Area.objects.filter(parent__isnull=True)
        else:
            return Area.objects.all()


    # serializer_class = AreaSerializer     #省
    # serializer_class = AreaSerializer1    #市 区县
    # 重写这个方法 我们可以根据具体的业务逻辑 返回不同的序列化器

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return AreaSerializer1









